package cn.getech.data.development.controller.search;


import cn.getech.data.development.entity.dataMap.BdpTableUseInfo;
import cn.getech.data.development.entity.dataMap.SearchHistory;
import cn.getech.data.development.model.req.search.DataSearchReq;
import cn.getech.data.development.service.SearchHistoryService;
import cn.getech.data.development.utils.SqlParseUtil;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.utils.ShiroUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 数据查询
 * @author: wangzhaowen：kiss
 * @create: 2020/12/1 16:27
 * @version: 2020/12/1
 **/
@Api(tags = "查询数据")
@RestController
@RequestMapping("/bdp/data/search")
public class DataSearchController {

    @Autowired
    public SearchHistoryService searchHistoryService;


    @ApiOperation(value = "保存使用说明")
    @SysLog(value = "保存使用说明", type = OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    @PostMapping("/save/use")   //  /data-development/bdp/data/search/save/use
    public R saveUseInfo(@RequestBody BdpTableUseInfo param) {
        if (param.getTableId() == null) {
            R.error("表id不能为空");
        }
        searchHistoryService.saveUseInfo(param);
        return R.okWithData(param);
    }

    @ApiOperation(value = "根据条件查询集合")
    @SysLog(value = "详情", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    @PostMapping("/detail")   //  /data-development/bdp/data/search/detail
    public R detail(@RequestBody SearchDetail param) {
        if (param.getId() == null) {
            R.error("表id不能为空");
        }
        if (param.getTableType() == null) {
            R.error("表类型不能为空");
        }
        JSONObject data = searchHistoryService.searchDetail(param);
        return R.okWithData(data);
    }
    @ApiOperation(value = "根据条件查询集合")
    @SysLog(value = "搜索记录", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    @PostMapping("/history")   //  /data-development/bdp/data/search/history
    public R history(@RequestBody SearchHistory param ) {
        if (StringUtils.isEmpty(param.getSearchType())&&StringUtils.isEmpty(param.getSearchClass())) {
            return R.error("数据类型不能为空");
        }
        //查询类型,当类型为数据源。数据服务，采集任务，传searchClass等效于传searchType
        if(StringUtils.isEmpty(param.getSearchType())){
            param.setSearchType(param.getSearchClass());
        }
        List<SearchHistory> list= searchHistoryService.historyList(param);
        return R.okWithData(list);
    }
    @ApiOperation(value = "产生记录")
    //@SysLog(value = "产生记录", type = OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    @PostMapping("/record")   //  /data-development/bdp/data/search/record
    public R record(@RequestBody SearchHistory param ) {
        if (StringUtils.isEmpty(param.getSearchType())&&StringUtils.isEmpty(param.getSearchClass())) {
            return R.error("数据类型不能为空");
        }
        //查询类型,当类型为数据源。数据服务，采集任务，传searchClass等效于传searchType
        if(StringUtils.isEmpty(param.getSearchType())){
            param.setSearchType(param.getSearchClass());
        }
        searchHistoryService.saveHistory(param);
        return R.ok();
    }

    @ApiOperation("数据搜索")
    @PostMapping("/page")//  /data-development/bdp/data/search/page
    @SysLog(value = "数据搜索", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R searchPage(@RequestBody DataSearchReq params) {
        if (params.getPage() == null) {
            params.setPage(1);
        }
        if (params.getLimit() == null) {
            params.setLimit(10);
        }
        if (StringUtils.isNotEmpty(params.getFieldName())) {
            String s = params.getFieldName().replace("_", "\\_");
            params.setFieldName(s);
        }if (StringUtils.isNotEmpty(params.getTableName())) {
            String s = params.getTableName().replace("_", "\\_");
            params.setTableName(s);
        }
        if (StringUtils.isNotEmpty(params.getTargetName())) {
            String s = params.getTargetName().replace("_", "\\_");
            params.setTargetName(s);
        }
        if (StringUtils.isNotEmpty(params.getDataSource())) {
            String s = params.getDataSource().replace("_", "\\_");
            params.setDataSource(s);
        }
        if (StringUtils.isNotEmpty(params.getDataService())) {
            String s = params.getDataService().replace("_", "\\_");
            params.setDataService(s);
        }
        if (StringUtils.isNotEmpty(params.getCollectTask())) {
            String s = params.getCollectTask().replace("_", "\\_");
            params.setCollectTask(s);
        }
        JSONObject data = searchHistoryService.searchPage(params);
        return R.okWithData(data);
    }

    @ApiOperation("sql语法检查")
    @PostMapping("/check/sql")//  /data-development/bdp/data/search/check/sql
    @SysLog(value = "sql语法检查", type = OperationTypeEnum.CHECKCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.DATA_EXPLORE_MODULE_CODE)
    public R searchPage(@RequestBody SearchDetail params) {
        if (StringUtils.isEmpty(params.getSql())) {
            return R.error("sql语句为空,无法校验");
        }
        String sql=params.getSql();
        sql = sql.replaceAll("\\$\\{", "");
        sql = sql.replaceAll("\\}", "");
        sql= SqlParseUtil.wrapSql(sql);
        String dbType = JdbcConstants.MYSQL;
        if (params.getDbType().equals(1)) {
            dbType = JdbcConstants.MYSQL;
        } else if (params.getDbType().equals(2)) {
            dbType = JdbcConstants.ORACLE;
        } else if (params.getDbType().equals(3)) {
            //由于HIVE 语法太严格  用mysql 验证
            dbType = JdbcConstants.MYSQL;
        }
        String res = SqlParseUtil.verifySql(sql, dbType);
        if (StringUtils.isNotEmpty(res)) {
            return R.error(res);
        }
        return R.ok();
    }
}

